<?php

/* autoload.php
 *
 * (C) 2012, StaySimple CMS team.
 *
 * This file is part of StaySimple.
 * http://code.google.com/p/stay-simple-cms/
 *
 * StaySimple is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * StaySimple is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with StaySimple.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The __autoload function tries to automatically load a class if it is not 
 * already loaded.
 * 
 * The autoload function uses an index stored in the global array $autoloadCache, 
 * created by <em>recreateAutoloadCache()</em>
 * 
 * @param string $name The class name to search for.
 */
function __autoload($name) {
    global $autoloadCache;

    if (isset($autoloadCache[strtolower($name)]) && is_file($autoloadCache[strtolower($name)])) {
        require $autoloadCache[strtolower($name)];
    }
}

/**
 * Recreate the index of class files
 */
function recreateAutoloadCache() {
    global $autoloadCache;
    $autoloadCache = array();

    $paths = array(
        APPROOT . '/core/',
        APPROOT . '/plugins/'
    );

    foreach ($paths as $dir) {
        $Directory = new RecursiveDirectoryIterator($dir);
        $Iterator = new RecursiveIteratorIterator($Directory);
        $Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

        foreach ($Regex as $filename)
            foreach ($filename as $filename) {
                $code = file_get_contents($filename);
                $code = preg_replace('!/\*.*?\*/!s', '', $code);
                $code = preg_replace('/\n\s*\n/', "\n", $code);

                $matches = array();
                preg_match_all('/class +([a-zA-Z0-9_]+)/', $code, $matches);
                if (isset($matches[1]))
                    foreach ($matches[1] as $match)
                        $autoloadCache[strtolower($match)] = $filename;

                $matches = array();
                preg_match_all('/interface +([a-zA-Z0-9_]+)/', $code, $matches);
                if (isset($matches[1]))
                    foreach ($matches[1] as $match)
                        $autoloadCache[strtolower($match)] = $filename;
            }
    }
    file_put_contents(DATAROOT . '/cache/autoload.cache', serialize($autoloadCache));
}

// load cache, if exists or create it
global $autoloadCache;
$autoloadCache = array();

if (is_file(DATAROOT . '/cache/autoload.cache')) {
    $autoloadCache = unserialize(file_get_contents(DATAROOT . '/cache/autoload.cache'));
    if (empty($autoloadCache)) {
        $autoloadCache = array();
        unlink(DATAROOT . '/cache/autoload.cache');
        recreateAutoloadCache();
    }
} else {
    recreateAutoloadCache();
}